package com.itc.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itc.common.core.constant.Constants;
import com.itc.common.core.domain.R;
import com.itc.common.core.utils.StringUtils;
import com.itc.common.core.utils.bean.BeanUtils;
import com.itc.common.security.utils.SecurityUtils;
import com.itc.system.api.domain.SysDictType;
import com.itc.system.domain.dto.SysDictTypeDto;
import com.itc.system.service.SysDictTypeService;
import com.itc.system.mapper.SysDictTypeMapper;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

/**
* @author zhangyh
* @description 针对表【sys_dict_type(字典类型表)】的数据库操作Service实现
* @createDate 2024-05-08 14:27:24
*/
@Service
public class SysDictTypeServiceImpl extends ServiceImpl<SysDictTypeMapper, SysDictType>
    implements SysDictTypeService{

    @Override
    public R insertDictType(SysDictTypeDto dict) {
        //校验字典名称和类型都不能重复
        Long count = this.baseMapper.selectCount(
                new LambdaQueryWrapper<SysDictType>()
                .eq(SysDictType::getDictName,dict.getDictName())
                .or()
                .eq(SysDictType::getDictType,dict.getDictType())
        );
        if(count > 0){
            return R.fail("名称或类型已存在");
        }
        SysDictType sysDictType = new SysDictType();
        BeanUtils.copyProperties(dict,sysDictType);
        sysDictType.setCreateBy(SecurityUtils.getUsername());
        sysDictType.setCreateTime(LocalDate.now().toString());
        sysDictType.setStatus(Constants.STRING_0);
        this.baseMapper.insert(sysDictType);
        return R.ok();
    }

    @Override
    public R updateDictType(SysDictTypeDto dict) {
        SysDictType sysDictType = this.baseMapper.selectById(dict.getDictId());
        if(sysDictType == null){
            return R.fail("字典类型信息不存在");
        }
        if(dict.getDictName() != null && !StringUtils.equals(dict.getDictName(),sysDictType.getDictName())){
            Long count = this.baseMapper.selectCount(
                    new LambdaQueryWrapper<SysDictType>()
                            .eq(SysDictType::getDictName,dict.getDictName())
            );
            if(count > 0){
                return R.fail("名称已存在");
            }
        }
        if(dict.getDictType() != null && !StringUtils.equals(dict.getDictType(),sysDictType.getDictType())){
            Long count = this.baseMapper.selectCount(
                    new LambdaQueryWrapper<SysDictType>()
                            .eq(SysDictType::getDictType,dict.getDictType())
            );
            if(count > 0){
                return R.fail("类型已存在");
            }
        }
        BeanUtils.copyProperties(dict,sysDictType);
        sysDictType.setUpdateBy(SecurityUtils.getUsername());
        sysDictType.setUpdateTime(LocalDate.now().toString());
        this.baseMapper.updateById(sysDictType);
        return R.ok();
    }

    @Override
    public R deleteDictTypeByIds(List<Long> dictIds) {
        this.removeBatchByIds(dictIds);
        return R.ok();
    }
}




